<?php

namespace App\Http\Controllers\Admin;

use App\Helpers\Pinyin;
use App\Models\District;

class DistrictController extends BaseController
{
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Exception
     */
    public function index(){
        if ($this->isOnSubmit()) {
            $delete = $this->request->input('delete');
            if ($delete) {
                foreach ($delete as $id) {
                    District::where('id', $id)->delete();
                }
            }

            $itemlist = $this->request->input('itemlist');
            if ($itemlist) {
                $pinyin = new Pinyin();
                foreach ($itemlist as $id=>$item){
                    if ($item['name']) {
                        if (!$item['letter']){
                            $item['letter'] = $pinyin->getFirstChar($item['name']);
                        }

                        if (!$item['pinyin']){
                            $item['pinyin'] = $pinyin->getPinyin($item['name']);
                        }

                        if ($id > 0) {
                            District::where('id', $id)->update($item);
                        }else {
                            District::insert($item);
                        }
                    }
                }
            }
            return $this->messager()->message(trans('sysmessage.info save success'))->render();
        }else {
            $province = intval($this->request->get('province'));
            $city     = intval($this->request->get('city'));
            $district = intval($this->request->get('district'));

            $provincelist = $citylist = $districtlist = $items = [];
            District::where('fid', 0)
                ->orderBy('displayorder')
                ->orderBy('id')->get()
                ->each(function ($p) use (&$provincelist){
                    $provincelist[$p->id] = $p;
                });
            $items = $provincelist;

            if ($province) {
                District::where('fid', $province)
                    ->orderBy('displayorder')
                    ->orderBy('id')
                    ->get()->each(function ($c) use (&$citylist){
                        $citylist[$c->id] = $c;
                    });
                $items = $citylist;
            }

            if ($city) {
                District::where('fid', $city)
                    ->orderBy('displayorder')
                    ->orderBy('id')
                    ->get()->each(function ($d) use (&$districtlist){
                        $districtlist[$d->id] = $d;
                    });
                $items = $districtlist;
            }

            if ($district) {
                $items = [];
                District::where('fid', $district)
                    ->orderBy('displayorder')
                    ->orderBy('id')
                    ->get()->each(function ($t) use (&$items){
                        $items[$t->id] = $t;
                    });
            }

            $this->assign(compact('province', 'city', 'district', 'provincelist', 'citylist', 'districtlist', 'items'));

            return $this->view('admin.common.district');
        }
    }
}
